using System;
using System.Data;
using Ent.AccesoDatos;
using System.Collections.Generic;
using System.Collections;
using Csla;

namespace Ent.Inscripcion.Core
{
    [Serializable]
    public class Catalogo : BusinessBase<Catalogo>
    {
        #region Business Methods

        // TODO: add your own fields, properties and methods

        #region < PROPIEDADES >
        private static PropertyInfo<Guid> IdProperty = RegisterProperty(typeof(Catalogo), new PropertyInfo<Guid>("Id"));
        private Guid _Id = IdProperty.DefaultValue;
        public Guid Id
        {
            get { return GetProperty(IdProperty, _Id); }
            set { SetProperty(IdProperty, ref _Id, value); }
        }
        
        public static readonly PropertyInfo<string> CodigoProperty = RegisterProperty<string>(c => c.Codigo);
        /// <Summary>
        /// Gets or sets the codigo value.
        /// </Summary>
        public string Codigo
        {
            get { return GetProperty(CodigoProperty); }
            set { SetProperty(CodigoProperty, value); }
        }

        public static readonly PropertyInfo<string> NombreProperty = RegisterProperty<string>(c => c.Nombre);
        /// <Summary>
        /// Gets or sets the nombre value.
        /// </Summary>
        public string Nombre
        {
            get { return GetProperty(NombreProperty); }
            set { SetProperty(NombreProperty, value); }
        }

        public static readonly PropertyInfo<string> DescripcionProperty = RegisterProperty<string>(c => c.Descripcion);
        /// <Summary>
        /// Gets or sets the Descripcion value.
        /// </Summary>
        public string Descripcion
        {
            get { return GetProperty(DescripcionProperty); }
            set { SetProperty(DescripcionProperty, value); }
        }

        private static PropertyInfo<string> TipoProperty = RegisterProperty<string>(o => o.Tipo, "Name");
        public string Tipo
        {
            get { return GetProperty(TipoProperty); }
            set { SetProperty(TipoProperty, value); }
            
        }

        private static PropertyInfo<CatalogoItems> ItemsProperty = RegisterProperty<CatalogoItems>(o => o.Items, "Name");
        public CatalogoItems Items
        {
            get
            {
                if (!(FieldManager.FieldExists(ItemsProperty)))
                    if (this.IsNew)
                        LoadProperty(ItemsProperty, CatalogoItems.NewCatalogoItems());
                    else
                        LoadProperty(ItemsProperty, CatalogoItems.GetCatalogoItems(ReadProperty(IdProperty)));
                return GetProperty(ItemsProperty);
            }
        }
        #endregion
        #endregion

        #region Validation Rules

        protected override void AddBusinessRules()
        {
            // TODO: add validation rules
            //ValidationRules.AddRule(RuleMethod, "");
        }

        #endregion

        #region Authorization Rules

        

        private static void AddObjectAuthorizationRules()
        {
            // TODO: add authorization rules
            //AuthorizationRules.AllowEdit(typeof(Catalogo), "Role");
        }

        #endregion

        #region Factory Methods

        public static Catalogo New()
        {
            return DataPortal.Create<Catalogo>();            
        }

        public static Catalogo Get(Guid id)
        {
            return DataPortal.Fetch<Catalogo>(id);
        }

        public static void Delete(Guid id)
        {
            DataPortal.Delete<Catalogo>(id);
        }

        //public static DataPortal getCodigoCatalogo() { 
        //return DataPortal.Fetch<Cod
        //}

        private Catalogo()
        {
            LoadProperty(IdProperty, Guid.NewGuid());
        }

        #endregion

        #region Data Access

        [RunLocal]
        protected override void DataPortal_Create()
        {
            // TODO: load default values
            // omit this override if you have no defaults to set
            base.DataPortal_Create();
        }

        private void DataPortal_Fetch(Guid criteria)
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.Mysql);
            var parametros = new List<Parametro> {new Parametro("Id", criteria)};
            var datos = accesoDatos.EjecutarReader("ConsultarCatalogo",parametros);
            if (datos.Datos.Read())
            {
                LoadProperty(IdProperty, Guid.Parse(datos.Datos.GetString(0)));
                LoadProperty(CodigoProperty, datos.Datos.GetString(1));
                LoadProperty(NombreProperty, datos.Datos.GetString(2));
                LoadProperty(DescripcionProperty, datos.Datos.GetString(3));
            }
            if (!datos.Datos.IsClosed) datos.Datos.Close();
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            try
            {
                var accesoDatos = new DriveDataBase(TypeDataBase.Mysql);
                List<Parametro> parametros = new List<Parametro>();                
                parametros.Add(new Parametro("Codigo", ReadProperty(CodigoProperty)));
                parametros.Add(new Parametro("Nombre", ReadProperty(NombreProperty)));
                parametros.Add(new Parametro("Descripcion", ReadProperty(DescripcionProperty)));
                parametros.Add(new Parametro("Id", ReadProperty(IdProperty)));
                accesoDatos.EjecutarQuery("InsertarCatalogo", parametros);
                FieldManager.UpdateChildren(ReadProperty(IdProperty));
            }
            catch{
                throw;
            }
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.Mysql);
            List<Parametro> parametros = new List<Parametro>();
            parametros.Add(new Parametro("Codigo", ReadProperty(CodigoProperty)));
            parametros.Add(new Parametro("Nombre", ReadProperty(NombreProperty)));
            parametros.Add(new Parametro("Descripcion", ReadProperty(DescripcionProperty)));
            parametros.Add(new Parametro("Id", ReadProperty(IdProperty)));
            accesoDatos.EjecutarQuery("ActualizarCatalogo", parametros);
            FieldManager.UpdateChildren(ReadProperty(IdProperty));
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(ReadProperty(IdProperty));
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Delete(Guid criteria)
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.Mysql);
            List<Parametro> parametros = new List<Parametro>();
            parametros.Add(new Parametro("Id", criteria));
            accesoDatos.EjecutarQuery("EliminarCatalogo", parametros);
        }

        #endregion
    }
}
